package com.transaction.common.base;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class BaseController {

    public Object retResultVo(){
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setCode(200);
        resultVo.setData(null);
        return resultVo;
    }

    public Object retResultVo(Object obj){
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setCode(200);
        resultVo.setData(obj);
        return resultVo;
    }

    public Object retResultVo(String msg){
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setCode(200);
        resultVo.setMsg(msg);
        return resultVo;
    }

    public Object retResultVo(String msg,Object obj){
        ResultVo<Object> resultVo = new ResultVo<>();
        resultVo.setCode(200);
        resultVo.setMsg(msg);
        resultVo.setData(obj);
        return resultVo;
    }

    public static void downLoadFilesToZip(HttpServletRequest request,
                                          HttpServletResponse response,
                                          String fileDir,
                                          String zipFileName){
        try{
            //设置响应头
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("multipart/form-data");

            //设置压缩包名字
            String agent = request.getHeader("USER-AGENT");
            if(agent.contains("MSIE") || agent.contains("Trident")){
                zipFileName = URLEncoder.encode(zipFileName,"UTF-8");
            }else{
                zipFileName = new String(zipFileName.getBytes("UTF-8"),"ISO-8859-1");
            }
            response.setHeader("Content-Disposition","attachment;fileName=\"" + zipFileName + "\"");

            //设置压缩刘写入response,边写边下载
            ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
            //设置压缩方式
            zipOut.setMethod(zipOut.DEFLATED);

            //获得需要写入的文件
            File file = new File(fileDir);
            File[] files = file.listFiles();

            DataOutputStream dataOut = null;
            //循环写入
            for (File textFile : files) {
                String fileName = textFile.getName();
                String filePath = fileDir + fileName;

                zipOut.putNextEntry(new ZipEntry(fileName));
                dataOut = new DataOutputStream(zipOut);

                InputStream inputS = new FileInputStream(new File(filePath));
                byte[] b = new byte[1024];
                int length = 0;
                while((length = inputS.read(b)) != -1){
                    dataOut.write(b,0,length);
                }
                inputS.close();
                zipOut.closeEntry();
            }
            dataOut.flush();
            dataOut.close();
            zipOut.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //上传文件
    public void writeUploadFile(MultipartFile file,String filePath){

        try {
            InputStream is = file.getInputStream();

            FileOutputStream os = new FileOutputStream(filePath + File.separator + file.getOriginalFilename());

            byte[] b = new byte[1024];
            int size = 0;

            while ((size = is.read(b)) != -1) {
                os.write(b, 0, size);
            }

            os.flush();
            os.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
